package com.fanfanwang.ffw.app.db;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.fanfanwang.ffw.app.AppContext;
import com.fanfanwang.ffw.app.bean.UserBean;
import com.fanfanwang.ffw.app.db.ItotemContract.Tables.UserBeanTable;

/**
 * @author Administrator
 * 
 */
public class DBUtil extends BaseDBUtil {
	//创建表操作
	public final static Uri USER_DB_URI = build(ItotemContract.USER_URI);

	// -------------------user----------------------
	public static void addUserBean(Context mContext, UserBean bean) {
		mContext.getContentResolver().insert(USER_DB_URI, bean.beanToValues());
	}

	public static ArrayList<UserBean> getUserList(Context mContext) {
		ArrayList<UserBean> list = new ArrayList<UserBean>();
		if(AppContext.user_id == 0){
			return null;
		}
		Cursor cursor = mContext.getContentResolver().query(USER_DB_URI, null, null, null, null);
		try {
			while (cursor.moveToNext()) {
				list.add(new UserBean().cursorToBean(cursor));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null)
				cursor.close();
		}
		return list;
	}
	
	/**
	 * 根据id查询用户
	 * 
	 * @param mContext
	 * @param albumId
	 * @return
	 */
	public static UserBean getUsrById(Context mContext, String userId) {
		UserBean userBean = null;
		Cursor cursor = mContext.getContentResolver().query(USER_DB_URI, null, UserBeanTable.USER_ID, new String[] { userId }, null);
		if (null == userBean) {
			userBean = new UserBean();
		}
		try {
			if (cursor.moveToNext()) {
				userBean.cursorToBean(cursor);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null)
				cursor.close();
		}
		return userBean;
	}
	
	public static void delUser(Context mContext, String user_id) {
		mContext.getContentResolver().delete(USER_DB_URI, user_id, null);
	}
	
	public static void delAllUser(Context mContext) {
		mContext.getContentResolver().delete(USER_DB_URI,null, null);
	}
	
	/**
	 * 更新数据
	 * 
	 * @param mContext
	 * @param bean
	 */
	public static void updateUser(Context mContext, UserBean bean) {
		mContext.getContentResolver().insert(USER_DB_URI, bean.beanToValues());
	}
}
